﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using LogLib;
using Newtonsoft.Json.Linq;

namespace Reader
{
    public class IDCardReaderS : IDevice
    {
        private DeviceState _state = DeviceState.NotInitialized;

        /// <summary>
        /// 指示设备的运行状态
        /// </summary>
        public DeviceState State
        {
            get
            {
                return _state;
            }
        }

        private int devId = -1;
        public int DeviceID
        {
            get
            {
                return devId;
            }
        }

        public string DeviceName
        {
            get
            {
                return this.GetType().Name;
            }
        }

        private bool _bInited = false;
        public bool Inited
        {
            get
            {
                return _bInited;
            }
        }
    
        private bool _bExit = false;
        public bool Exited
        {
            get
            {
                return _bExit;
            }
        }

        private ReaderDll readerDll = new ReaderDll();

        public IServer Server
        {
            get;
            set;
        }

        private string _sessionID = "";
        public string SessionID
        {
            get
            {
                return _sessionID;
            }
            set
            {
                _sessionID = value;
            }
        }

        public IntPtr WindowHandle
        {
            get;
            set;
        }

        private string _error = "";
        public string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        public IDCardReaderS()
        {
        }

        public void Test(string param)
        {

        }

        public void Init(string param)
        {
            Log.Write("Start Init");
            int ret = readerDll.InitReader();
            if (0 == ret)
            {
                _bInited = true;
                //SendMessage(index, MessageType.MSG_INIT_OK);
                Log.Write("Init Success");
            }
            else
            {
                _bInited = false;
                //SendMessage(index, MessageType.MSG_INIT_NK);
                Log.Write("Init Fail:" + ret.ToString());
            }
            Log.Write("End Init");
        }

        public void DoAction(string param)
        {
            Log.Write("Start DoPerso");
            JObject inParam = JObject.Parse(param);
            string phtoPath = (string)inParam["photoPath"];

            JObject outParam = null;
            ReaderDll.STR_ID_INFO id_info = new ReaderDll.STR_ID_INFO(); ;
            int ret = readerDll.ReadIdCard(phtoPath, out id_info);
            if (0 == ret)
            {
                outParam = new JObject();
                outParam.Add("ZJHM", id_info.idNum);
                outParam.Add("XM", id_info.name);
                outParam.Add("XB", id_info.sex);
                outParam.Add("MZ", id_info.people);
                outParam.Add("DZ", id_info.address);
                outParam.Add("CSRQ", id_info.birth);
                outParam.Add("QFDW", id_info.issuePlace);
                outParam.Add("QFSJ", id_info.issueDay);
                outParam.Add("SXSJ", id_info.expiryDay);

                //SendMessage(index, MessageType.MSG_DO_PERSO_OK, "", outParamString);
            }
            else
            {
                //SendMessage(index, MessageType.MSG_DO_PERSO_NK);
                Log.Write("DoPerso Fail:" + ret.ToString());
            }
            Log.Write("End DoPerso");
        }

        public void Start(string param)
        {
            if (_bInited)
            {
                _thread = new ReadCardThread(readerDll,  param);
                _thread.Start();
            }
            else
            {
                Log.Write("读写器未初始化");
            }
        }

        public void Stop(string param)
        {

        }

        public void Exit(string inParams)
        {
            if (_bInited)
            {
                readerDll.UnInitReader();
                _bInited = false;
                _bExit = true;
            }
            Log.Write("Device has exited.");
            // 向客户端发送状态
        }

        public void Response(byte[] packData)
        {

        }

        public void SendMessage(string msgPack)
        {

        }    

        public int StopPerso(int index, string inParamString, out string outParamString)
        {
            outParamString = "";
            if (null != _thread)
            {
                _thread.Stop();
            }
            Log.Write("StopPerso");
            return 0;
        }



            private string _inParamString = "";
        private bool _exitFlag = false;
        private ReaderDll _readerDll = null;
        Thread _thread;

        public CreateCardThread(ReaderDll readerDll,  string inParamString)
        {
            _readerDll = readerDll;
            _inParamString = inParamString;
            _thread = new Thread(new ThreadStart(this.ReadCard));
        }

        public void Start()
        {
            if (_thread != null)
            {
                _exitFlag = false;
                _thread.Start();
            }
        }

        public void Stop()
        {
            _exitFlag = true;
            if (_thread != null)
            {
                Thread.Sleep(1000);
                _thread.Abort();
            }
        }

        private void ReadCardInfo()
        {
            JObject inParam = JObject.Parse(_inParamString);
            string phtoPath = (string)inParam["photoPath"];
            JObject outParam = null;
            string outParamString = "";
            while (!_exitFlag)
            {
                ReaderDll.STR_ID_INFO id_info = new ReaderDll.STR_ID_INFO(); ;
                int ret = _readerDll.ReadIdCard(phtoPath, out id_info);
                if (0 == ret)
                {
                    outParam = new JObject();
                    outParam.Add("ZJHM", id_info.idNum);
                    outParam.Add("XM", id_info.name);
                    outParam.Add("XB", id_info.sex);
                    outParam.Add("MZ", id_info.people);
                    outParam.Add("DZ", id_info.address);
                    outParam.Add("CSRQ", id_info.birth);
                    outParam.Add("QFDW", id_info.issuePlace);
                    outParam.Add("QFSJ", id_info.issueDay);
                    outParam.Add("SXSJ", id_info.expiryDay);
                    outParamString = outParam.ToString();

                    // 在此添加处理

                    break;
                }              
                Thread.Sleep(500);
            }
        }
    }
}
